using System;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;

namespace TfsSpam.Differ.Configuration
{
    /// <summary>
    /// The TeamProject class represents a single project element in the configuration
    /// file and defines the name of a TFS project and the TFS path of the file containing
    /// the e-mail addresses of the people who should receive checkin notification e-mails.
    /// </summary>
    public class TeamProject : ConfigurationElement
    {
        #region -- Public properties
        /// <summary>
        /// The name of the TFS project.
        /// </summary>
        [ConfigurationProperty( "name", IsRequired = true, IsKey = true )]
        public string Name
        {
            get { return (string) base[ "name" ]; }
        }

        /// <summary>
        /// The TFS path of the text file containing the e-mail addresses of the
        /// people who should receive the checkin notification e-mails.
        /// </summary>
        [ConfigurationProperty( "subscribersItemPath", IsRequired = true )]
        public string SubscribersItemPath
        {
            get { return (string) base[ "subscribersItemPath" ]; }
        }
        #endregion
    }

    /// <summary>
    /// The TeamProjects class represents the list of project elements
    /// defined in the configuration file.
    /// </summary>
    public class TeamProjects : ConfigurationSection
    {
        #region -- Public properties
        [ConfigurationProperty( "serverUrl", IsRequired = true )]
        public Uri ServerUrl
        {
            get { return (Uri) base[ "serverUrl" ]; }
        }

        /// <summary>
        /// The collection of <see cref="TeamProject"/> elements defined
        /// in the configuration file.
        /// </summary>
        [ConfigurationProperty( "", IsDefaultCollection = true )]
        public TeamProjectCollection Projects
        {
            get { return (TeamProjectCollection) base[ "" ]; }
        }
        #endregion
    }

    /// <summary>
    /// The TeamProjectCollection class represents a collection of project elements
    /// in the configuration file and defines the list of TFS projects for which
    /// checkin notification e-mails should be sent out and the information about the
    /// recipients of these e-mails.
    /// </summary>
    [SuppressMessage( "Microsoft.Design", "CA1010", Justification = "This is the standard way of implementing a configuration element collection" )]
    [ConfigurationCollection( typeof( TeamProject ), AddItemName = "project", CollectionType = ConfigurationElementCollectionType.BasicMap )]
    public class TeamProjectCollection : ConfigurationElementCollection
    {
        #region -- Base class overrides
        /// <summary>
        /// Creates a new <see cref="TeamProject"/> object to be included
        /// in the collection.
        /// </summary>
        /// <returns>A new object to include in the collection.</returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new TeamProject();
        }

        /// <summary>
        /// Retrieves the key for an element in the collection.
        /// </summary>
        /// <param name="element">The element whose key should be retrieved.</param>
        /// <returns>The key for the specified element.</returns>
        protected override object GetElementKey( ConfigurationElement element )
        {
            return ( (TeamProject) element ).Name;
        }
        #endregion
    }
}
